public class TruncateSentence {
    public String truncateSentence(String s, int k) {
        int length = s.length();
        int end = length;
        for(int i = 0;i<length;i++){
            if(k == 0){
                end = i-1;
                break;
            }
            if(s.charAt(i) == ' '){
                k--;
            }
        }
        return s.substring(0,end);
    }
}
